const userModel = require('../../models/userModel')

const userService = {
    // 用户登录数据库操作模块
    login: async({ username,password}) => {
        return userModel.find(
         {
            username,
            password
         }
        )
    },
    // 用户上数据更新模块
    upload: async(data) => {
        const { username, password, _id, gender, introduction, avatar} = data
        if (avatar) {
            return userModel.updateOne({
                _id
            }, {
                username,
                password,
                gender,
                introduction,
                avatar
            })
        }else {
            return userModel.updateOne({
                _id
            }, {
                username,
                password,
                gender,
                introduction,
            })
        }
    },
    // 用户添加模块
    add: async (data) => {
        const { username, password, _id, gender, introduction, avatar, role} = data
        //    调用数据库添加数据
        console.log('312334');
        return userModel.create({
                username,
                password,
                gender,
                introduction,
                avatar, 
                role
            })
       
    },
    // 用户列表获取模块
    list: async () => {
        return userModel.find({}, ["username", "avatar", "role", "_id", "introduction", "gender"])
    },
    // 用户删除模块
    delete: async (id) => {
        return userModel.deleteOne({ _id: id })
    },
    // 获取编辑用户信息
    editGet: async (id) => {
        // return id
        return userModel.find({ _id: id}, ["username",  "role", "_id", "introduction", "password"])
    },
    // 提交用户数据
    editSumbit: (data) => {
        const { username, password, _id,  introduction,  role}  = data
        console.log(data);
        return userModel.updateOne({_id}, {
            username,
            password,
            introduction,
            role
        })
    }
}

module.exports = userService;